//
//  EMSwitch.m
//  Test0202
//
//  Created by 史伟文 on 15/2/2.
//  Copyright (c) 2015年 cczu. All rights reserved.
//

#import "EMSwitch.h"

@implementation EMSwitch
{
    CGRect rect;
    
}
- (void)setTitle:(NSString *)title
{
    _title = title;
    [self createSwitch];
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        rect = frame;
        _isOn = NO;
        
    }
    return self;
}

- (void)createSwitch
{
    CGRect itemRect;
    itemRect.origin.x = 0;
    itemRect.origin.y = 0;
    itemRect.size.width = 20;
    itemRect.size.height = 20;
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = itemRect;
    [button setImage:[UIImage imageNamed:@"icon_没选中"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonTap:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:button];
    itemRect.origin.x = 25;
    itemRect.origin.y = 0;
    itemRect.size.width = 60;
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:itemRect];
    titleLabel.text = self.title;
    titleLabel.textColor = [UIColor grayColor];
    titleLabel.font = [UIFont fontWithName:HEITILIGHT size:14];
    [self addSubview:titleLabel];
}

- (void)buttonTap:(UIButton *)sender
{
    if (_isOn) {
        [sender setImage:[UIImage imageNamed:@"icon_没选中"] forState:UIControlStateNormal];
    } else {
        [sender setImage:[UIImage imageNamed:@"icon_选中喜爱的"] forState:UIControlStateNormal];
    }
    _isOn = !_isOn;
    if (self.actionBlock)
        self.actionBlock(self.isOn);
}
@end
